package Util;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import POJO.Books;

public class DataProduct {
	
	public static Books product(Integer id) throws UnsupportedEncodingException{
		Books book = new Books();
		book.setId(id);
		book.setAutor(getAthor());
		book.setName(getName());
		book.setNew(getIsNew());
		book.setPrice(getPrice());
		book.setPubDate(getDate());
		book.setPublisher(getPublisher());
		return book;
	}
	
	/**
	 * 生成常见的一级汉字
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getChinese() throws UnsupportedEncodingException{
		String str = null;
		byte[] b = new byte[2];
		
		int high,low;
		Random random = new Random();
		high = 176 + random.nextInt(71);
		low = 161 + random.nextInt(93);
		
		b[0] = (byte) high;
		b[1] = (byte) low;
		
		str = new String(b,"GB2312");
		return str;
	}
	
	/**
	 * 生成出版社
	 * @return
	 */
	public static String getPublisher(){
		String[] publisher = { "人民出版社","中央编译出版社","化学工业出版社","电子工业出版社", "中国经济出版社","人民法院出版社"};
		int n = (int) Math.round((Math.random()*5));
		return publisher[n];
		
	}
	
	/**
	 * 生成1990年到2013年随机日期
	 * @return
	 */
	public static Date getDate(){
		Calendar calendar = Calendar.getInstance();  
	       //注意月份要减去1  
	       calendar.set(1990,11,31);  
	       calendar.getTime().getTime();  
	       //根据需求，这里要将时分秒设置为0  
	       calendar.set(Calendar.HOUR_OF_DAY, 0);  
	       calendar.set(Calendar.MINUTE, 0);  
	       calendar.set(Calendar.SECOND,0);  
	       long min = calendar.getTime().getTime();;  
	       calendar.set(2013,11,31);  
	       calendar.set(Calendar.HOUR_OF_DAY,0);  
	       calendar.set(Calendar.MINUTE,0);  
	       calendar.set(Calendar.SECOND,0);  
	       calendar.getTime().getTime();  
	       long max = calendar.getTime().getTime(); 
	       long time = Math.round(Math.random()*(max-min)+min);
	       return new Date(time);
	}
	
	/**
	 * 随机生成价钱
	 * @return
	 */
	public static double getPrice(){
		DecimalFormat df = new DecimalFormat("###0.00");
		double price = 10 + Math.random()*200;
		
		return Double.valueOf(df.format(price));
	}
	
	/**
	 * 生成作者
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getAthor() throws UnsupportedEncodingException{
		StringBuilder sb = new StringBuilder();
		int len = (int) (2+Math.round(Math.random()));
		for (int i = 0; i < len; i++) {
			sb.append(getChinese());
		}
		return sb.toString();
	}
	
	/**
	 * 生成书名
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getName() throws UnsupportedEncodingException{
		StringBuilder sb = new StringBuilder();
		int len = (int) (2+Math.round(Math.random()*7));
		for (int i = 0; i < len; i++) {
			sb.append(getChinese());
		}
		return sb.toString();
	}
	
	public static boolean getIsNew(){
		if(Math.random()>0.5){
			return true;
		}else{
			return false;
		}
	}
	
}
